package com.transcasafe.eanpay.core.http;


import com.transcasafe.eanpay.core.exception.PayApiException;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 请求支付API，请求头上下文
 */
@Slf4j
@Getter
@ToString
public class PayApiHttpHeadContext {
    private final String mchNo;
    private final String signatureHeader;
    private final String algorithm;
    private final String version;
    private final String timestamp;
    private final String nonce;
    private final String signature;


    private PayApiHttpHeadContext(String mchNo,
                                  String signatureHeader,
                                  String algorithm,
                                  String version,
                                  String timestamp,
                                  String nonce,
                                  String signature) {
        this.mchNo = mchNo;
        this.signatureHeader = signatureHeader;
        this.algorithm = algorithm;
        this.version = version;
        this.timestamp = timestamp;
        this.nonce = nonce;
        this.signature = signature;
    }

    public static class Builder {
        private String mchNo;
        private String signatureHeader;
        private String timestamp;
        private String nonce;


        public Builder mchNo(String mchNo) {
            this.mchNo = mchNo;
            return this;
        }

        public Builder signatureHeader(String signatureHeader) {
            this.signatureHeader = signatureHeader;
            return this;
        }

        public Builder nonce(String nonce) {
            this.nonce = nonce;
            return this;
        }

        public Builder timestamp(String timestamp) {
            this.timestamp = timestamp;
            return this;
        }

        public PayApiHttpHeadContext build() {
            if (StringUtils.isBlank(mchNo)) {
                throw new PayApiException("HTTP请求头mchNo为空");
            }

            if (StringUtils.isBlank(nonce)) {
                throw new PayApiException("HTTP请求头nonce为空");
            }

            if (StringUtils.isBlank(timestamp)) {
                throw new PayApiException("HTTP请求头timestamp为空");
            }

            if (StringUtils.isBlank(this.signatureHeader)) {
                throw new PayApiException("HTTP请求头SIGNATURE为空");
            }

            try {
                String[] msgArray = StringUtils.split(signatureHeader, ",");
                Map<String, String> map = new HashMap<>();
                for (String keyValue : msgArray) {
                    map.put(StringUtils.substringBefore(keyValue, "="), StringUtils.substringAfter(keyValue, "="));
                }
                return new PayApiHttpHeadContext(mchNo, signatureHeader, map.get("algorithm"), map.get("version"), timestamp, nonce, map.get("signature"));
            } catch (Exception e) {
                log.error("authorization parse error,signatureHeader={}", signatureHeader, e);
                throw new PayApiException("HTTP请求authorization格式错误");
            }
        }
    }
}
